<?php
namespace BackHome\Controller;
use Think\Controller;
class VadminController extends CommonController {
    public function index(){
        $village_admin = M('village_admin'); // 实例化对象
		$count      = $village_admin->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $village_admin->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$newlist=array();
		foreach($list as $one){
			//后面次id被覆盖，所以重新保存
			$one['village_admin_id']=$one['id'];
			//原生态写法-查乡村信息
			$Model = M();
			$sql="SELECT (select name from `tv_area` where id=v.area_lev_1)as a1,(select name from `tv_area` where id=v.area_lev_2)as a2,(select name from `tv_area` where id=v.area_lev_3)as a3,(select name from `tv_area` where id=v.area_lev_4)as a4,(select name from `tv_area` where id=v.area_lev_5)as a5,v.* FROM `tv_village` v WHERE id=".$one['v_id']." order by id desc";
			$getall=$Model->query($sql);
			
			//查第一书记微信信息
			$user=D('user');
			$getwx=$user->where("id=".$one['user_id'])->find();
			//查第一书记姓名
			$user_info=D('user_info');
			$getinfo=$user_info->where("u_id=".$one['user_id'])->find();
			if($getinfo){
				
				}else{
					//插入一条书记信息
					$data['u_id']=$one['user_id'];
					$user_info->add($data);
				}//end if
			
			$newlist[]=array_merge($one,$getall[0],$getwx,(array)$getinfo);//合并数组其中一个为null，结果为null，需要强制类型转换(array)
		}//end for
		//dump($newlist);
		$this->assign('list',$newlist);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		//dump($show);
		$this->display(); // 输出模板
    }//end fun
	
	public function del(){
        $village_admin = M('village_admin'); // 实例化对象
		$id=$_GET['id'];
		$village_admin->where('id='.$id)->delete();
		$this->success('删除成功', '/BackHome/Vadmin/');
	}//end fun 
	
	//检索村庄
	public function search_v(){
		$keyword=$_POST['vkeyword'];
		if($keyword=='')$this->ajaxReturn("0");
		//原生态写法
		$Model = M();
		$sql="SELECT (select name from `tv_area` where id=v.area_lev_1)as a1,(select name from `tv_area` where id=v.area_lev_2)as a2,(select name from `tv_area` where id=v.area_lev_3)as a3,(select name from `tv_area` where id=v.area_lev_4)as a4,(select name from `tv_area` where id=v.area_lev_5)as a5,v.* FROM `tv_village` v WHERE v.`title` LIKE  '%".$keyword."%' order by id desc";
		$getall=$Model->query($sql);
		
		if(count($getall)>=1){
			$this->ajaxReturn($getall,'json');
			}else{
				$this->ajaxReturn("0");
			}
	}//end fun
	
	//检索用户
	public function search_u(){
		$keyword=$_POST['ukeyword'];
		if($keyword=='')$this->ajaxReturn("0");
		//原生态写法
		$Model = M();
		$sql="SELECT v.* FROM `tv_user` v WHERE v.`nickname` LIKE  '%".$keyword."%' order by id desc";
		$getall=$Model->query($sql);
		
		if(count($getall)>=1){
			$this->ajaxReturn($getall,'json');
			}else{
				$this->ajaxReturn("0");
			}
	}//end fun
	
	//绑定村和书记
	public function binding(){
		$village_admin=D('village_admin');//dump($_POST);
		$vid=$_POST['vid'];
		$uid=$_POST['uid'];
		$data=array();
		foreach($vid as $one){
			if($one==0)continue;
			$num=$village_admin->where("v_id=".$one." and user_id=".$uid)->count();
			if($num==0){
				//可以插入
				$data['v_id']=$one;
				$data['user_id']=$uid;
				$alt=$village_admin->add($data);
				if($alt>0){
					$redata['ok']++;
					}else{
						$redata['sqlerr']++;
					}
				}else{
					//已经存在
					$redata['err']++;
				}
		}//end for
		$this->ajaxReturn($redata,'json');
	}//end fun
	
	//编辑书记信息
	public function va_edit(){
		$user_info=D('user_info');
		$getinfo=$user_info->where("u_id=".$_GET['id'])->find();
		$this->assign('info',$getinfo);// 
		$this->display(); // 输出模
	}//end fun 
	//更新书记
	public function va_upd(){
		$user_info=D('user_info');
			$data=array();
			//单文件上传
			if($_FILES['img']["name"]!=''){
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize   =     3145728 ;// 设置附件上传大小
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->rootPath  =      './Public/Uploads/'; // 设置附件上传根目录
				$upload->autoSub = false;	//关闭日期类型子目录自动生成
				$upload->savePath  =      'user_info/'; // 设置附件上传（子）目录
				// 上传单个文件 
				$info   =   $upload->uploadOne($_FILES['img']);
				if(!$info) {// 上传错误提示错误信息
					$this->error($upload->getError());
				}else{// 上传成功 获取上传文件信息
					 $data['img']=$info['savepath'].$info['savename'];
				}
			}
		$data['id']=$_POST['id'];
		$data['name']=$_POST['name'];
		$data['info']=$_POST['info'];
		$data['supports']=$_POST['supports'];
		$data['clicks']=$_POST['clicks'];
		$alt=$user_info->save($data);
			if($alt>0){
				$this->success('更新成功', '/BackHome/Vadmin/');
				}else{
						//错误页面的默认跳转页面是返回前一页，通常不需要设置
						$this->error('更新失败');
				}
	}//end fun
}